<template>
	<view>
		<view class="img">
			头像<u-avatar :src="avatarUrl" @click="img" size="140" mode="circle"></u-avatar>
		</view>
		<view class="list">
			<view class="title">
				昵称
			</view>
			<view class="input">
				<input type="text" v-model="username" />
			</view>
		</view>
		<view class="list">
			<view class="title">
				个人签名
			</view>
			<view class="input">
				<input type="text" v-model="user_note" />
			</view>
		</view>
		<view class="list">
			<view class="title">
				教务管理系统账号
			</view>
			<view class="input">
				<input type="number" v-model="jwczh" />
			</view>
		</view>
		<view class="list">
			<view class="title">
				教务管理系统密码
			</view>
			<view class="input">
				<input type="password" v-model="password" />
			</view>
		</view>
		<view class="warn">
			{{warn}}
		</view>
		<button type="primary" @click="submit">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatarUrl: '',
				username: '',
				user_note: '',
				jwczh: '',
				password: '',
				warn: ''
			}
		},
		onLoad() {
			this.avatarUrl = uni.getStorageSync('avatarUrl');
			this.username = uni.getStorageSync('nickName');
			this.user_note = uni.getStorageSync('userNote');
			this.jwczh = uni.getStorageSync('jwczh');
			this.password = uni.getStorageSync('password');
		},
		methods: {
			img() {
				var that = this;
				uni.chooseImage({
					count: 1,
					success: function(res) {
						that.avatarUrl = res.tempFilePaths[0]
						console.log(res.tempFilePaths[0]);
						uni.uploadFile({
							url: 'https://wesl.nekolab.tech/sp/v1/login/headImg',
							filePath: res.tempFilePaths[0],
							name: 'headImg',
							success: (uploadFileRes) => {
								that.avatarUrl = JSON.parse(uploadFileRes.data).data.filePath
								console.log(JSON.parse(uploadFileRes.data).data.filePath);
							}
						});
					}
				});
			},
			submit() {
				switch (1) {
					case 1: {
						const regUsername = /^[\u4e00-\u9fa5_a-zA-Z0-9\s\·\~\！\@\#\￥\%\……\&\*\（\）\——\-\+\=\【\】\{\}\、\|\；\‘\’\：\“\”\《\》\？\，\。\、\`\~\!\#\$\%\^\&\*\(\)\_\[\]{\}\\\|\;\'\'\:\"\"\,\.\/\<\>\?]{1,10}$/;
						if (regUsername.test(this.username)) {} else {
							this.warn = '昵称不能为空且不能超过十个字';
							break;
						}
					}
					case 2: {
						const regUserNote = /^[\u4e00-\u9fa5_a-zA-Z0-9\s\·\~\！\@\#\￥\%\……\&\*\（\）\——\-\+\=\【\】\{\}\、\|\；\‘\’\：\“\”\《\》\？\，\。\、\`\~\!\#\$\%\^\&\*\(\)\_\[\]{\}\\\|\;\'\'\:\"\"\,\.\/\<\>\?]{1,50}$/;
						if (regUserNote.test(this.user_note)) {} else {
							this.warn = '个人签名不能为空且不能超过50字';
							break; 
						}
					}
					case 3: {
						if (this.jwczh == '' || this.jwczh == null) {
							this.warn = '教务管理系统账号不能为空';
							break;
						} else if (this.password == '' || this.password == null) {
							this.warn = '教务管理系统密码不能为空'
							break;
						}
					}
					case 4: {
						this.warn = '';
						this.$request({
							method: 'POST', // 默认GET
							url: '/v1/login/userInfoWithUrlheadImg',
							header: {
								'Content-Type': 'application/x-www-form-urlencoded'
							},
							data: {
								token: uni.getStorageSync('token'),
								username: this.username,
								user_note: this.user_note,
								jwczh: this.jwczh,
								password: this.password,
								headImg: this.avatarUrl
							},
						}).then(res => {
							console.log(res.message.code)
							if (res.message.code != 200) {
								this.warn = res.message.msg;
							} else {
								uni.setStorage({
									key: 'nickName',
									data: this.username
								})
								uni.setStorage({
									key: 'userNote',
									data: this.user_note
								})
								uni.setStorage({
									key: 'avatarUrl',
									data: this.avatarUrl
								})
								uni.setStorage({
									key: 'jwczh',
									data: this.jwczh
								})
								uni.setStorage({
									key: 'password',
									data: this.password
								})
								uni.switchTab({
									url: './mine',
									success: function(e) {
										var page = getCurrentPages()[0]
										if (page == undefined || page == null) return;
										page.onLoad();
									}
								})
							}
						}).catch(res => {
							// 失败后执行
						})
					}
				}
			}
		}
	}
</script>

<style>
	.img {
		padding-left: 36rpx;
		line-height: 212rpx;
		background-color: white;
		border-bottom: 1rpx solid #DBDBDB;
		font-weight: 700;
	}

	.u-avatar {
		float: right;
		margin: 36rpx 24rpx;
	}

	.list {
		background-color: white;
		border-bottom: 1rpx solid #DBDBDB;
		display: flex;
		padding: 36rpx;
	}

	.title {
		flex: 1;
		font-weight: 700;
	}

	.content {
		flex: 1;
	}

	input {
		border-bottom: 2rpx solid #DCDFE6;
	}

	.warn {
		margin: 12rpx 0 0 36rpx;
		color: #cc3300;
		font-size: 28rpx;
	}

	button {
		margin-top: 100rpx;
		width: 50%;
	}
</style>
